package com.xucy.springboot.designmodel.structural.Proxy.Dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author xucy
 * @Date 2019-07-28 09:05
 * @Description
 **/

public class DynamicProxy implements InvocationHandler {

    Object target;

    DynamicProxy(Object object){
        this.target=object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        before();
        Object object=null;
        try {
            object=method.invoke(target,args);

            after("提交");
        }catch (Exception e){
            after("回滚");
            throw new RuntimeException("我回滚了");
        }

        return object;
    }

    public void before(){
        System.out.println("开启事务");
    }

    public void after(String msg){
        System.out.println(msg+"事务");
    }
}
